<?php
/**
 * html.inc.php
 */

if(!defined('IN_SITE') || !defined('IN_ADMIN')) {
	exit('Access Denied');
}

$op = in_array($op, array('index', 'content', 'shop', 'category')) ? $op : 'content';

@set_time_limit(1000);
$html = load('html.class.php');

switch ($op)
{
	case 'index':
		$filesize = $html->index();
	    showmessage('网站首页更新成功！<br />大小：'.sizecount($filesize),'?action=html');
		break;
		
	case 'content':
		if($dosubmit)
		{
			if($type == 'last')
			{
				$offset = 0;
			}
			else
			{
				$page = max(1, intval($page));
				$offset = $pagesize*($page-1);
			}
		    $where = ' WHERE `status`>0 AND `islink`=0 ';
			$order = 'ASC';
			
			if ($type == 'last' && $num) //更新最后发布的 $num 条
			{
				$offset = 0;
				$pagesize = $num;
				$order = 'DESC';
			}
			elseif($type == 'id')
			{
				$fromid = intval($fromid);
				$toid = intval($toid);
				if($fromid) $where .= " AND `contentid`>=$fromid ";
				if($toid) $where .= " AND `contentid`<=$toid ";
			}
			elseif($type == 'date')
			{
				if($fromdate)
				{
					$fromtime = strtotime($fromdate.' 00:00:00');
					$where .= " AND `pubtime`>=$fromtime ";
				}
				if($todate)
				{
					$totime = strtotime($todate.' 23:59:59');
					$where .= " AND `pubtime`<=$totime ";
				}
			}
			elseif ($type == 'all')
			{
				$total = $db->result_first("SELECT count(*) FROM `{$tablepre}content` $where");
				$pages = ceil($total/$pagesize);
			}

			$query = $db->query("SELECT `contentid`, `special` FROM `{$tablepre}content` $where ORDER BY `contentid` $order LIMIT $offset,$pagesize");
			while ($row = $db->fetch_array($query)) {
				$data[] = $row;
			}

			foreach($data as $r)
			{
				if ($r['special'] == 1)
				{
					$html->zhuanti($r['contentid']);
				}
				else
				{
					$html->content($r['contentid']);
				}
			}
			if($pages > $page)
			{
				$page++;
				$creatednum = $offset + count($data);
				$percent = round($creatednum/$total, 2)*100;
				$message = "共需更新 <font color='red'>$total</font> 条信息<br />已完成 <font color='red'>{$creatednum}</font> 条（<font color='red'>{$percent}%</font>）";
				$forward = "?action=html&op=content&dosubmit=1&type=$type&fromid=$fromid&toid=$toid&fromdate=$fromdate&todate=$todate&pagesize=$pagesize&page=$page&pages=$pages&total=$total";
			}
			else
			{
				$message = "更新完成！";
				$forward = '?action=html&op=content';
			}
			showmessage($message, $forward);
		}
		else
		{
			include admin_tpl("creathtml");
		}
		break;
	
	case 'category':
		if($dosubmit)
		{
			$page = max(1, intval($page));
			$path = $db->result_first("SELECT `path` FROM {$tablepre}category WHERE cid='$cid'");
			$filesize = $catetype==1 ? $html->category($cid,$catetype,$path,$page,12) : $html->category($cid,$catetype,$path,$page,14);
		}
		else
		{
			include SITE_ROOT.'data/cache/cache_category.php';
			include admin_tpl("creathtml");
		}
		break;
		break;
		
	default:
		break;
}
?>